package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Notify;
import com.example.elderlycare.dao.mapper.NotifyMapper;
import com.example.elderlycare.service.NotifyService;
import com.example.elderlycare.service.entity.NotifyItem;
import com.example.elderlycare.service.entity.NotifyListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class NotifyServiceImpl extends ServiceImpl<NotifyMapper, Notify>
        implements NotifyService {
    @Resource
    NotifyMapper notifyMapper;

    @Override
    public NotifyListResponseBody queryNotifies(int current, int pageSize, QueryWrapper<Notify> queryWrapper) {
        Page<Notify> page = new Page<>(current, pageSize);
        Page<Notify> notifyPage = notifyMapper.selectPage(page, queryWrapper);
        List<NotifyItem> notifyItemList = new ArrayList<>();
        notifyPage.getRecords().forEach(notify -> {
            NotifyItem item = NotifyItem.builder()
                    .id(notify.getId())
                    .content(notify.getContent())
                    .department(notify.getDepartment())
                    .publisher(notify.getPublisher())
                    .releaseTime(notify.getReleaseTime())
                    .title(notify.getTitle())
                    .build();
            notifyItemList.add(item);
        });
        return NotifyListResponseBody.builder()
                .total((int) notifyPage.getTotal())
                .notifyItemList(notifyItemList)
                .build();
    }
}
